//
//  TMGGuideViewController.m
//  TMGold
//
//  Created by coder on 2018/3/19.
//  Copyright © 2018年 tangmu. All rights reserved.
//

#import "STTools_GuideViewController.h"
#import "STTools_GuideViewController.h"
#import "STTools_PageView.h"
@interface STTools_GuideViewController ()<UIScrollViewDelegate>
@property(nonatomic, strong) STPageControl                    *control;/**<  */
@property(nonatomic, strong) STTools_PageView                     *pageView;
@property(nonatomic, strong) YYLabel                      *chatLable;/**< u */
@property(nonatomic, strong) UIView                       *contentView;/**<  */
@property(nonatomic, strong) STButton                     *agreeButton;/**<  */
@property(nonatomic, strong) UIScrollView                     *scrollView;/**<  */
@end

@implementation STTools_GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self stoneobs_ui_configSubView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)stoneobs_ui_configSubView{
    //    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:SHOW_WEALCOME_KEY];
    //    [[NSUserDefaults standardUserDefaults] synchronize];
    __weak typeof(self) weakSelf =  self;
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    self.scrollView.backgroundColor = UIColor.whiteColor;
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    NSArray * array = @[[UIImage imageNamed:@"happy_game_qd_1"],
                        [UIImage imageNamed:@"happy_game_qd_2"],
                        [UIImage imageNamed:@"happy_game_qd_3"],
    [UIImage imageNamed:@"happy_game_qd_6"]].mutableCopy;
    for (NSInteger i = 0; i < array.count; i ++) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
        imageView.left = STONEOBS_UI_UISCREEN_WIDTH * i;
        imageView.image = array[i];
        [self.scrollView addSubview:imageView];
        self.scrollView.contentSize = CGSizeMake(imageView.right, 0);
    }
    
    STPageControl * control = [[STPageControl alloc] initWithPages:array.count handle:^(NSInteger tag) {
        
    }];
    control.pageSize = CGSizeMake(9, 9);
    control.currentPageSize = CGSizeMake(9, 9);
    [self.view addSubview:control];
    control.pageIndicatorTintColor = FlatGray;
    control.currentPageIndicatorTintColor = WH_UICOLOR_FROM_RGBA(0x3A74E2);
    control.centerX = STONEOBS_UI_UISCREEN_WIDTH /2;
    control.bottom = self.view.height-140;
    self.control = control;
    
    //    ({
    //
    //
    //        self.pageView = [[STTools_PageView alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH, STONEOBS_UI_UISCREEN_HEIGHT)];
    //        self.pageView.autoMoveDuring = 0;
    //        self.pageView.pageControl.hidden = YES;
    //        self.pageView.backgroundColor = UIColor.whiteColor;
    //        self.pageView.imageArray = @[[UIImage imageNamed:@"happy_game_qidongye_1"],
    //                                     [UIImage imageNamed:@"happy_game_qidongye_2"],
    //                                     [UIImage imageNamed:@"happy_game_qidongye_3"],
    //                                     [UIImage imageNamed:@"happy_game_qidongye_4"],
    //                                     [UIImage imageNamed:@"happy_game_qidongye_6"],].mutableCopy;
    //        self.pageView.animationType = FSPagerViewTransformerTypeNone;
    //        [self.pageView setOnSelectedBannerHandle:^(NSInteger index) {
    //            //        CMBannerModel * banner =  weakSelf.bannerArray[index];
    //            //        NSString * url = banner.url;
    //            //        if (!url.length) {
    //            //            url = banner.img;
    //            //        }
    //            //        STTools_WebViewController * web = [[STTools_WebViewController alloc] initWithUrl:url];
    //            //        web.forceTitle = @"广告";
    //            //        [weakSelf pushViewController:web animated:YES];
    //        }];
    //        [self.view addSubview:self.pageView];
    //        [self.pageView setScrollDidScroll:^(NSInteger currentIndex) {
    //            weakSelf.control.currentPage = currentIndex;
    //
    //            if (currentIndex > 3) {
    //                [weakSelf createContentView];
    //                weakSelf.pageView.userInteractionEnabled = NO;
    //                weakSelf.control.hidden = YES;
    //            }
    //
    //
    //        }];
    //        STPageControl * control = [[STPageControl alloc] initWithPages:self.pageView.imageArray.count handle:^(NSInteger tag) {
    //
    //        }];
    //        control.pageSize = CGSizeMake(9, 9);
    //        control.currentPageSize = CGSizeMake(9, 9);
    //        [self.pageView.superview addSubview:control];
    //        control.pageIndicatorTintColor = FlatGray;
    //        control.currentPageIndicatorTintColor = WH_UICOLOR_FROM_RGBA(0x3A74E2);
    //        control.centerX = STONEOBS_UI_UISCREEN_WIDTH /2;
    //        control.bottom = self.pageView.bottom-140;
    //        self.control = control;
    //
    //    });
    //
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger currentIndex = scrollView.contentOffset.x / STONEOBS_UI_UISCREEN_WIDTH;
    self.control.currentPage = currentIndex;
    
    if (currentIndex > 2) {
        [self createContentView];
        self.scrollView.userInteractionEnabled = NO;
        self.control.hidden = YES;
    }
}
- (void)createContentView{
    [self.contentView removeFromSuperview];
    if (self.contentView) {
        [self.view addSubview:self.contentView];
        return;
    }
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH, 200)];
    view.bottom = STONEOBS_UI_UISCREEN_HEIGHT;
    self.contentView = view;
    __weak typeof(self) weakSelf =  self;
    [self.view addSubview:view];
    UIColor * buttonBgColor = [UIColor colorWithRed:94/255.0 green:97/255.0 blue:232/255.0 alpha:1];
    STButton * button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 160, 42)
                                                  title:@"立即开启"
                                             titleColor:UIColor.whiteColor
                                              titleFont:18
                                           cornerRadius:21
                                        backgroundColor:buttonBgColor
                                        backgroundImage:nil
                                                  image:nil];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [view addSubview:button];
    button.centerX = STONEOBS_UI_UISCREEN_WIDTH/2;
    [button stoneobs_fun_touch_handle:^(UIButton *sender) {
        [weakSelf stoneobs_action_goSlectedGoIngButton];
    }];
    STButton * cbutton = [[STButton alloc] initWithFrame:CGRectMake(15, button.bottom + 20, 40, 40)
                                                   title:@""
                                              titleColor:UIColor.whiteColor
                                               titleFont:15
                                            cornerRadius:0
                                         backgroundColor:nil
                                         backgroundImage:nil
                                                   image:[UIImage imageNamed:@"happy_game_wh_btn_unselected"]];
    cbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//    cbutton.selected = YES;
    [cbutton setImage:[UIImage imageNamed:@"happy_game_wh_btn_selected"] forState:UIControlStateSelected];
    [cbutton stoneobs_fun_touch_handle:^(UIButton *sender) {
        sender.selected = !sender.selected ;
    }];
    
    button.centerX = STONEOBS_UI_UISCREEN_WIDTH/2;
    self.agreeButton = cbutton;
    
    self.chatLable = [YYLabel new];
    self.chatLable.numberOfLines = 0;
    [view addSubview:self.chatLable];
    self.chatLable.layer.cornerRadius = 4;
    self.chatLable.clipsToBounds = YES;
    self.chatLable.displaysAsynchronously=YES;
    self.chatLable.userInteractionEnabled = YES;
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSMutableAttributedString  *resultAttr = [[NSMutableAttributedString alloc] initWithString:@""];
        resultAttr.alignment = NSTextAlignmentLeft;
        resultAttr.lineSpacing = 3;
        ({
            NSMutableAttributedString *headerText = [[NSMutableAttributedString alloc] initWithString:@"同意并接受"];
            [headerText setColor:STONEOBS_UI_FIRST_TEXT_COLOR range:headerText.rangeOfAll];
            headerText.font = [UIFont systemFontOfSize:15];
            [resultAttr appendAttributedString:headerText];
        });
        ({
            NSString * title = @"《用户协议》";
            NSMutableAttributedString *headerText = [[NSMutableAttributedString alloc] initWithString:title];
            [headerText setColor:buttonBgColor range:headerText.rangeOfAll];
            headerText.font = [UIFont systemFontOfSize:15];
            [resultAttr appendAttributedString:headerText];
            
            ({
                YYTextHighlight *highlight = [YYTextHighlight new];
                [highlight setColor:[UIColor whiteColor]];
                highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
                    
 
                    
                };
                NSString * nameTitle = resultAttr.string;
                NSRange range = [nameTitle rangeOfString:title];
                [resultAttr setTextHighlight:highlight range:range];
            });
        });
        ({
            NSMutableAttributedString *headerText = [[NSMutableAttributedString alloc] initWithString:@"和"];
            [headerText setColor:STONEOBS_UI_FIRST_TEXT_COLOR range:headerText.rangeOfAll];
            headerText.font = [UIFont systemFontOfSize:15];
            [resultAttr appendAttributedString:headerText];
        });
        ({
            
            NSString * title = @"《隐私协议》";
            NSMutableAttributedString *headerText = [[NSMutableAttributedString alloc] initWithString:title];
            [headerText setColor:buttonBgColor range:headerText.rangeOfAll];
            headerText.font = [UIFont systemFontOfSize:15];
            [resultAttr appendAttributedString:headerText];
            
            ({
                YYTextHighlight *highlight = [YYTextHighlight new];
                [highlight setColor:[UIColor whiteColor]];
                highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
                    

                    
                };
                NSString * nameTitle = resultAttr.string;
                NSRange range = [nameTitle rangeOfString:title];
                [resultAttr setTextHighlight:highlight range:range];
            });
            
        });
        
        YYTextContainer *titleContarer = [YYTextContainer new];
        titleContarer.size = CGSizeMake(CGFLOAT_MAX,40);
        
        YYTextLayout *titleLayout = [YYTextLayout layoutWithContainer:titleContarer text:resultAttr];
        CGFloat titleLabelWith= titleLayout.textBoundingSize.width;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.chatLable.textLayout = titleLayout;
            self.chatLable.frame = CGRectMake(15, button.bottom + 20, titleLabelWith+10, 24);
            self.chatLable.centerX = STONEOBS_UI_UISCREEN_WIDTH/2 + 20;
            cbutton.right = self.chatLable.left;
            cbutton.centerY = self.chatLable.centerY;
            [view addSubview:cbutton];
            
        });
    });
    
    
    
}
- (void)stoneobs_action_goSlectedGoIngButton{
    if (!self.agreeButton.selected) {
        [SVProgressHUD showInfoWithStatus:@"请同意用户协议和隐私协议"];
        return;
    }
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:SHOW_WEALCOME_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    UIWindow * window = UIApplication.sharedApplication.delegate.window;
    RTRootNavigationController * nav = [[RTRootNavigationController alloc] initWithRootViewController:UIViewController.new];
    window.rootViewController = nav;
    [window stoneobs_fun_st_showAnimationWithType:STAnimationTyperippleEffect_stoneobs_enum duration:3];
}
@end
